package com.northpool.operator.common.algorithm;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import com.northpool.operator.common.minio.MinIOUtil;

import java.io.File;

public class JarUploadRepository {

    public static String s3Endpoint = "http://northpool.work:9000";
    public static String bucket = "stp-minio";
    public static String s3AccessKey = "northpool.stp";
    public static String s3SecretKey = "z6FFgHDXmzRq8sCAcqIiYgcOWfgDHRFCUc7fR1zK";

    public static MinIOUtil minIOUtil = new MinIOUtil(s3Endpoint, s3AccessKey, s3SecretKey);

    public static void main(String[] args) {
        String modelName = args[0];
        String version = args[1];

        if (args.length > 2 && StrUtil.isNotBlank(args[2])) {
            s3Endpoint = args[2];
            bucket = args[3];
            s3AccessKey = args[4];
            s3SecretKey = args[5];
        }
        Console.log("对象存储地址： {}", s3Endpoint);
        minIOUtil = new MinIOUtil(s3Endpoint, s3AccessKey, s3SecretKey);

        uploadJars(modelName, version);

        System.exit(0);
    }

    public static void uploadJars(String moduleName, String version) {
        String userDir = System.getProperty("user.dir");
        String modulePath = userDir + File.separator + moduleName;
        String jarFileName = moduleName + "-" + version + ".jar";
        String jarPath = modulePath + File.separator + "build" + File.separator + "libs" + File.separator + jarFileName;
        File jarFile = new File(jarPath);
        if (!jarFile.exists()) {
            throw new RuntimeException("jar文件不存在：" + jarPath);
        }
        String objectKey = StrUtil.format("apps/piesat/{}/{}/{}", moduleName, version, jarFileName);
        // 要先删除，防止对象的唯一标识不重新生成

        minIOUtil.removeObject(bucket, objectKey);
        // 上传jar
        minIOUtil.putObject(bucket, objectKey, FileUtil.getInputStream(jarFile));
        Console.log("上传完成: {}",objectKey);
    }
}
